//=========================================================================
// Copyright (C) 2024 The C++ Component Model(COMO) Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//=========================================================================

/**
 * Persist Memory allocator
 */

#ifndef PMALLOC_H
#define PMALLOC_H 1

#include <assert.h>
#include <config.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>

#define PMALLOC_MIN_POOL_SIZE \
    ((size_t)(1024 * 1024 * 16)) /* min pool size: 16MB */

//#define PMALLOC_KEYS        255

#ifdef __cplusplus
extern "C"
{
#endif

extern void *pmopen(const char *fn, void *baseaddr, size_t initial_size);
extern void pmclose(void *md);
extern long pmcapacity(void *md);
extern void *pmalloc(void *md, size_t size);
extern void *pmalloc_check(void *md, const size_t size, void *desc);
extern void *pmrealloc(void *md, void *ptr, const size_t size);
extern void *pmrealloc_check(void *md, void *ptr, const size_t size, void *desc);
extern void *pmcalloc(void *md, const size_t nmemb, const size_t size);
extern void *pmcalloc_check(void *md, const size_t num, const size_t size, void *desc);
extern void pmfree(void *md, void *addr);
extern void pmfree_check(void *md, void *ptr, void *desc);
extern void *pmalign(void *md, const size_t alignment, const size_t size);
extern void *pmvalloc(void *md, const size_t size);
extern void pmcheck(void *md, void (*func)(void *, void *, int));
extern struct mempoolstats pmstats(void *);
extern void *pmalloc_attach(int fd, void *baseaddr, const size_t initial_size, bool fixSized);
extern void *pmalloc_detach(void *md);
extern int pmalloc_setkey(void *md, const int keyidx, void *key);
extern void *pmalloc_getkey(void *md, const int keyidx);
extern int pmalloc_errno(void *);
extern int pmtrace(void);
extern int pmsync(void *md, void *addr, const size_t length);
extern void *p_addr(void *md, void *addr);
extern void *e_addr(void *md, void *addr);
extern void *b_addr(void *md);

#ifdef __cplusplus
}
#endif

#endif /* PMALLOC_H */
